package com.nbcb.nwql.applicantion.util;

import com.jd.platform.async.callback.ICallback;
import com.jd.platform.async.callback.IWorker;
import com.jd.platform.async.worker.WorkResult;
import com.jd.platform.async.wrapper.WorkerWrapper;
import com.nbcb.nwql.common.util.CommonUtil1;

import java.util.Date;
import java.util.Map;

/**
 * @author wuweifeng wrote on 2019-11-20.
 */
public class ParWorker implements IWorker<String, String>, ICallback<String, String> {

    /**
     * 在这里做耗时操作，如rpc请求、IO等
     *
     * @param object      object
     * @param allWrappers
     */
    @Override
    public String action(String object, Map<String, WorkerWrapper> allWrappers) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "当前时间: " + CommonUtil1.sdf2414.format(new Date()) + "  action入参object： " + object ;
    }

    @Override
    public String defaultValue() {
        return "threadVal";
    }

    @Override
    public void begin() {
        System.out.println(Thread.currentThread().getName() + "_____start");
    }

    @Override
    public void result(boolean success, String param, WorkResult<String> workResult) {
        if (success) {
            System.out.println("执行成功--" + CommonUtil1.sdf2414.format(new Date()) + "_____WorkResult_result: "
                    + workResult.getResult() + "_____threadName:" +Thread.currentThread().getName());
        } else {
            System.err.println("执行失败--" + CommonUtil1.sdf2414.format(new Date()) + "_____WorkResult_result: "
                    + workResult.getResult() + "_____threadName:" +Thread.currentThread().getName());
        }
    }

}